<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Upload extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('upload');
        $this->load->library('image_lib');
    }

    public function do_upload() {

        //图片上传配置
        $upload_config['upload_path'] = './uploads/gallery/source_img/';
        $upload_config['allowed_types'] = 'gif|jpg|png';
        //$upload_config['max_size'] = 0; //无限制,KB单位
        $upload_config['file_name'] = time() . mt_rand(100, 999);
        $this->upload->initialize($upload_config);

        if (!$this->upload->do_upload('file')) {
            $source_error = $this->upload->display_errors();
//			var_dump($source_error);
            echo FALSE;
        } else {
            $source_data = $this->upload->data();
//            var_dump($source_data);exit();
            //缩略小图生成配置
            $thumb_config['image_library'] = 'gd2';
            $thumb_config['source_image'] = $source_data['full_path'];
            $thumb_config['dynamic_output'] = FALSE;
            $thumb_config['quality'] = '70%';
            $thumb_config['new_image'] = './uploads/gallery/thumb_img/thumb_small/';
            $thumb_config['width'] = 100;
            $thumb_config['height'] = 100;
            $thumb_config['create_thumb'] = TRUE;
            $thumb_config['thumb_marker'] = '_thumb';
            $thumb_config['maintain_ratio'] = TRUE;
            $thumb_config['master_dim'] = 'auto';
            $this->image_lib->initialize($thumb_config);
            $small = $this->image_lib->resize();
            //缩略大图生成配置
            $thumb1_config['image_library'] = 'gd2';
            $thumb1_config['source_image'] = $source_data['full_path'];
            $thumb1_config['dynamic_output'] = FALSE;
            $thumb1_config['quality'] = '30%';
            $thumb1_config['new_image'] = './uploads/gallery/thumb_img/thumb_large/';
            $thumb1_config['width'] = $source_data['image_width']/2;
            $thumb1_config['height'] = $source_data['image_height']/2;
//            $thumb1_config['width'] = $source_data['image_width'];
//            $thumb1_config['height'] = 400;
            $thumb1_config['create_thumb'] = TRUE;
            $thumb1_config['thumb_marker'] = '_thumb';
            $thumb1_config['maintain_ratio'] = TRUE;
            $thumb1_config['master_dim'] = 'auto';
            $this->image_lib->initialize($thumb1_config);
            $large = $this->image_lib->resize();
            if (!$small || !$large) {
                $thumb_error = $this->image_lib->display_errors();
                echo FALSE;
            } else {
                //录入数据库
                $query = array(
                    'thumb_src' => '/uploads/gallery/thumb_img/thumb_small/' . $source_data['raw_name'] . '_thumb' . $source_data['file_ext'],
                    'photo_src' => '/uploads/gallery/thumb_img/thumb_large/' . $source_data['raw_name'] . '_thumb' . $source_data['file_ext'],
                    'photo_create_time' => time(),
                );
                $ret = $this->db->insert('ci_gallery', $query);
                echo $ret;
            }
            // 重置所有之前用于处理图片的值
            // 当你用循环来处理一批图片时，你可能会想使用它
            $this->image_lib->clear();
            //var_dump($source_data);
        }
    }

}
